<template>
  <div class="product">
    <div class="product_box" v-if="shopBagData.length > 0">
      <div class="info-box" v-for="(item, i) in shopBagData" :key="item.sid">
        <van-swipe-cell :disabled="isText">
          <div class="dot">
            <van-checkbox
              v-model="item.isChecked"
              checked-color="#7F2F21"
              @click="singleCheck"
            ></van-checkbox>
          </div>
          <van-card
            :price="item.price"
            :desc="item.rule"
            :title="item.name"
            class="goods-card"
            :thumb="item.small_img"
          >
            <template #footer>
              <div class="num">
                <van-stepper
                  v-model="item.count"
                  theme="round"
                  button-size="22"
                  disable-input
                  @change="updateProductCount(item.sid, item.count)"
                />
              </div>
            </template>
          </van-card>
          <template #right>
            <van-button
              square
              text="删除"
              type="danger"
              class="delete-button"
              @click="dele([item.sid], i)"
            />
          </template>
        </van-swipe-cell>
      </div>
    </div>
 
    <van-submit-bar
      v-show="!isText"
      :price="total"
      button-text="提交订单"
      button-color="#7F2F21"
    >
      <van-checkbox v-model="checkedAll" @click="allChoice">全选</van-checkbox>
    </van-submit-bar>
    <van-submit-bar
      v-show="isText"
      button-text="删除选择"
      button-color="#7F2F21"
      @submit="deleChoice"
    >
      <van-checkbox v-model="checkedAll" @click="allChoice">全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
import "../assets/less/product.less";
export default {
  props: ["isText", "shopBagData"],
  name: "Product",
  data() {
    return {
      // 全选或者取消全选
      checkedAll: false,

      // 总金额
      total: 0,
    };
  },
  created(){
    console.log(111)
    console.log(this.shopBagData)
  },
  computed: {
    ...mapState(["shopBag"]),
  },
  methods: {
    ...mapMutations(["changeShopBag"]),
    // 更新产品数据
    updateProductCount(sid, count) {
      let tokenString = this.$cookies.get("tokenString");

      //当用户未登录的时候跳转到登录页面
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        method: "post",
        url: this.baseUrl + "/modifyShopcartCount",
        data: {
          appkey: this.appkey,
          tokenString: tokenString,
          sid,
          count,
        },
      })
        .then((result) => {
          console.log(result);
          this.$toast.clear();
          if (result.data.code === 700) {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }
          if (result.data.code === 6000) {
            this.sum();
          } else {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log(err);
        });
    },

    // 全选
    allChoice() {
      this.shopBagData.forEach((v) => {
        v.isChecked = this.checkedAll;
      });
      // 计算金额
      this.sum();
    },

    // 单选
    singleCheck() {
      // 计算金额
      this.sum();
      if (this.shopBagData.length === 0) {
        this.checkedAll = false;
        return;
      }
      for (let i = 0; i < this.shopBagData.length; i++) {
        if (!this.shopBagData[i].isChecked) {
          this.checkedAll = false;
          return;
        }
      }
      this.checkedAll = true;
    },

    // 删除
    dele(sids, i) {
      // this.shopBagData.splice(i,1);
      // return;
      console.log(sids, i);
      let tokenString = this.$cookies.get("tokenString");

      //当用户未登录的时候跳转到登录页面
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        method: "post",
        url: this.baseUrl + "/deleteShopcart",
        data: {
          appkey: this.appkey,
          tokenString: tokenString,
          sids: JSON.stringify(sids),
        },
      })
        .then((result) => {
          console.log(result);
          this.$toast.clear();
          if (result.data.code === 700) {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }
          if (result.data.code === 7000) {
            // 单个删除
            if (i !== undefined) {
              this.shopBagData.splice(i, 1);
              this.changeShopBag(this.shopBag - 1);
              this.singleCheck();
              console.log(this.shopBagData);
            } else {
              // 多个删除
              for (let k = 0; k < this.shopBagData.length; k++) {
                if (this.shopBagData[k].isChecked) {
                  this.shopBagData.splice(k, 1);
                  k--;
                }
              }
              this.changeShopBag(this.shopBag - sids.length);
              this.sum();
            }
          } else {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log(err);
        });
    },

    // 删除选择的咖啡
    deleChoice() {
      let sids = [];
      this.shopBagData.forEach((v) => {
        console.log(v);
        if (v.isChecked) {
          sids.push(v.sid);
        }
      });

      if (sids.length === 0) {
        this.$toast({
          message: "请选择删除的商品",
          forbidClick: true,
          duration: 2000,
        });
        return;
      }
      console.log(sids);
      this.dele(sids);
    },

    // 计算金额
    sum() {
      let total = 0;
      this.shopBagData.forEach((v) => {
        if (v.isChecked) {
          total += v.count * v.price;
        }
      });
      this.total = total * 100;
    },
  },
};
</script>
